//
//  LBXListTableView.m
//  MacDevTest
//
//  Created by lbxia on 2020/8/15.
//  Copyright © 2020 com.lbx. All rights reserved.
//

#import "LBXFriendsTableView.h"
#import "JJScroller.h"
#import "FriendListView.h"
#import "CustomRowView.h"

@interface LBXFriendsTableView()<NSTabViewDelegate,NSTableViewDataSource,FriendListViewDelegate>
@property (nonatomic,strong)  NSTableView *tableView;

@end

@implementation LBXFriendsTableView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

- (id)initWithFrame:(NSRect)frameRect
{
    if (self = [super initWithFrame:frameRect])
    {
        [self tableView];
    }
    return self;
}

- (NSTableView*)tableView
{
    if (!_tableView) {
        
        _tableView = [[NSTableView alloc]initWithFrame:self.bounds];
             NSTableColumn * column = [[NSTableColumn alloc]initWithIdentifier:@"col1"];
             column.title = @"col1";
             column.minWidth = 200;
             column.maxWidth = 200;
             column.headerCell = [[NSTableHeaderCell alloc]initTextCell:@"11"];
             column.headerCell.backgroundColor = [NSColor clearColor];
             [_tableView addTableColumn:column];

             //tableview单列模拟iOS封装，通过继承NSTableView，监听鼠标事件，返回相关事件通知
         //    _tableView rowAtPoint:<#(NSPoint)#>
             
             //隐藏列表头 列的名称
             [_tableView setHeaderView:nil];
             
             
             self.contentView.documentView = _tableView;
    }
    return _tableView;
}

- (void)viewWillMoveToWindow:(NSWindow *)newWindow
{
    [super viewWillMoveToWindow:newWindow];
    
    self.borderType = NSNoBorder;
    
    //自定义滚动条，没有背景色，静止时，自动隐藏
       self.verticalScroller = [[JJScroller alloc]init];
       self.hasVerticalScroller  = YES;
     

        //Assigning to 'id<NSTableViewDelegate> _Nullable' from incompatible type 'NormalTableView *const __strong'
        _tableView.delegate = (id)self;
        _tableView.dataSource = self;
       
    
}

- (void)reloadData
{
    NSInteger selectedRow = [self.tableView selectedRow];

    [self.tableView reloadData];
    
    //恢复指定选中项
    if ( selectedRow >=0  ) {
        [self.tableView selectRowIndexes:[[NSIndexSet alloc]initWithIndex:selectedRow] byExtendingSelection:NO];
    }
}

- (void)viewDidMoveToWindow
{
    [super viewDidMoveToWindow];
    
     [_tableView reloadData];
    
//    [self addEvent];
}


#pragma mark- delegate

//  - （BOOL）validateProposedFirstResponder：（NSResponder *）responder forEvent：（NSEvent *） event
// {
// //这允许用户点击单元格中的控件，首先必须选择单元格行
// return YES;
//}

- (BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event
{
    return YES;
}
 

- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    return YES;
}

- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    NSLog(@"willSelectTabViewItem");
}

- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem
{
    NSLog(@"didSelectTabViewItem :%@",tabViewItem.identifier);
}

- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)tabView
{
    NSLog(@"tabViewDidChangeNumberOfTabViewItems");
}

#pragma mark - NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.listDatas.count;
    
}

// 设置行高
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row{
    return 60;
}



- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
    //修改选中颜色
    CustomRowView *rowView = [[CustomRowView alloc] init];
    return rowView;
}

//view-based模式(基于视图)

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    
//    NSView *view = [[NSView alloc]initWithFrame:NSMakeRect(0, 0, 300, 40)];
//    view.wantsLayer = YES;
//    view.layer.backgroundColor = [NSColor blackColor].CGColor;
//    return view;
    
//    NSArray* array = self.listDatas[row];

    
    FriendListView *fView = [[FriendListView alloc]initWithFrame:NSMakeRect(0, 0, 220, 40)];
    
//    fView.userImageView.image = [NSImage imageNamed:NSImageNameUser];
    
    fView.userImageView.image = [NSImage imageNamed:@"personalImg"];
    

//    fView.userNickName.stringValue = array.firstObject;
    fView.userNickName.stringValue = [NSString stringWithFormat:@"朋友昵称:%ld",(long)row];
    
    
    fView.detailInfo.stringValue = [NSString stringWithFormat:@"detaininfo"];
    
    

    return fView;
}

@end
